package com.yzy.rabbit;

import com.rabbitmq.client.Channel;
import com.yzy.config.SpringContextUtil;
import com.yzy.event.MyMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2022/7/14 10:14
 */
public class CustomerListener extends MyMessageListener {
    private static final Logger logger = LoggerFactory.getLogger(CustomerListener.class);

    @Override
    public void processMessage(Message message, Channel channel) {

        try {
            String msg = "";

            Jackson2JsonMessageConverter messageConverter = SpringContextUtil.getBean(Jackson2JsonMessageConverter.class);
            msg = (String) messageConverter.fromMessage(message);
            logger.info("监听处理消息:"+msg);
            //long deliveryTag = message.getMessageProperties().getDeliveryTag();
            //channel.basicReject(deliveryTag, false); //确认消息消费失败

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        }catch (Exception e){
            try {
                //异常、将信息丢回队列
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (Exception ex) {
                logger.error("队列异常", ex);
            }
        }
    }
}
